// 2013-8-1下午06:32:38  net.betou.trade.admin.action.CloseTradeAct.java

package net.betou.trade.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.TradeAct;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Trade;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author 叶装装
 */
@Controller
public class CloseTradeAct extends TradeAct< Trade >{

	private static final String DIRECTORY = "trade/close";  
	
	@RequestMapping( "/tradeclose/v_list.jspx" )
	public String list( Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		return super.toList( false, pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, request, response, model );
	}
	
	
	@RequestMapping( "/tradeclose/v_add.jspx" )
	public String v_add( Integer id, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		WebErrors errors = validateToSave( id, admin );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		model.addAttribute( "groupList", groupMng.getAllowReadList( null, null, null, null ) );
		model.addAttribute( "bean", manager.findMyTrade( id, admin.getId( ), admin.getSystem( )) );
		
		return getAddPage( );
	}
	
	@RequestMapping( "/tradeclose/o_save.jspx" )
	public String o_save( Trade bean, Integer id, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateToSave( id, admin );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.saveClose(  bean );
		
		logMng.operating( 0, request, Messages.LOG_CLOSE_TRADE, "id=" + bean.getId( ) );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping( "/tradeclose/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		super.toDelete( false, ids, response, request );
		return;
	}
	
	@RequestMapping ( "/tradeclose/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){
		return super.toEdit( request, id, model );
	}
	
	@RequestMapping ( "/tradeclose/o_update.jspx" )
	public String o_update( Integer id, Trade bean, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateOUpdate( id, admin.getSystem( ), admin.getId( ), request );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		bean = manager.updateClose( bean );
		
		logMng.operating( 1, request, Messages.LOG_CLOSE_TRADE, "id=" + bean.getId( ) );
		
		return "redirect:v_list.jspx";
	}
	
	
	
	private WebErrors validateToSave( Integer id, AgentAndAdmins admin ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		boolean system = admin.getSystem( );
		if( system){
			errors.addError( Messages.TRADE_CLOSE_ADMIN_SYSTEM_MANAGER_NOT_TO_ADD );
			return errors;
		}
		
		Trade bean = manager.findMyTrade( id, admin.getId( ), system );
		
		if( bean == null ){
			errors.addError( system ? Messages.DATA_NOT_EXIST : Messages.DATA_NOT_OPERATING);
			return errors;
		}
		
		return errors;
	}
	
	
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}
}
